﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EmployeeManagement
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            BindGv();
        }

        private void BindGv()
        {
            //1、定义sql语句
            string sql = "select EmpID,EmpName,Sex,Age,Tel,DeptName from Dept inner join Emp on Dept.DeptID=Emp.DeptID";
            //2、调用dbHelper中的查询方法
            DataSet ds = dbHelper.GetData(sql);
            //3、将数据绑定在gridview控件上
            this.dataGridView1.DataSource = ds.Tables[0];
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            //1、从前台获取用户输入的姓名和电话
            string name = this.txtName.Text;
            string tel = this.txtTel.Text;
            //2、定义sql语句
            string sql = "select EmpID,EmpName,Sex,Age,Tel,DeptName from Dept inner join Emp on Dept.DeptID=Emp.DeptID where 1=1";
            //判断  根据不同的查询情况添加查询条件
            //姓名不为空
            if (!string.IsNullOrEmpty(name))       //不为空：false  !false=true
            {
                sql = sql + $" and EmpName='{name}'";
            }
            //电话不为空
            if (!string.IsNullOrEmpty(tel))       //不为空：false  !false=true
            {
                sql = sql + $" and Tel='{tel}'";
            }
            //3、调用dbHelper中的查询方法
            DataSet ds = dbHelper.GetData(sql);
            //4、将数据绑定在gridview控件上
            this.dataGridView1.DataSource = ds.Tables[0];
            //判断数据集中是否为空
            if (ds.Tables[0].Rows.Count==0)
            {
                MessageBox.Show("没有满足查询条件的数据，请重新输入查询条件！");
            }
        }
        /// <summary>
        /// 删除选中员工的点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteRow_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你确定要删除吗？", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information)==DialogResult.Yes)
            {
                //从前台获取选中行的员工ID
                string id = this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells["EmpID"].Value.ToString();
                //sql命令
                string sql = $"delete from Emp where EmpID={id}";
                try
                {
                    //调用dbHelper中的增删改方法
                    bool boo = dbHelper.ExecuteNonQuery(sql);
                    //判断 是否删除成功
                    if (boo)
                    {
                        MessageBox.Show("删除成功！");
                        //重新绑定gridview
                        BindGv();
                    }
                    else
                    {
                        MessageBox.Show("删除失败！");
                    }
                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message);
                }
            }
           
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            EmpEditForm frm = new EmpEditForm();
            frm.Text = "新增员工";
            //打开另外一个窗体
            frm.ShowDialog();
            //重新绑定gridview
            BindGv();
        }
        /// <summary>
        /// 双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //获取前台用户选中行的员工ID
            string id = this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells["EmpID"].Value.ToString();
            //实例化窗体对象
            EmpEditForm frm = new EmpEditForm(id);
            frm.Text = "修改员工";
            frm.ShowDialog();
            //重新绑定gridview
            BindGv();
        }
    }
}
